home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / GNU_KIT / DISK8.ZIP / src / makest / implicit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-08  |  17.7 KB  |  587 lines

  1. /* Implicit rule searching for GNU Make.
  2. Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 1, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "rule.h"
  21. #include "dep.h"
  22. #include "file.h"
  23.  
  24. static int pattern_search ();
  25.  
  26. /* For a FILE which has no commands specified, try to figure out some
  27.    from the implicit pattern rules.
  28.    Returns 1 if a suitable implicit rule was found,
  29.    after modifying FILE to contain the appropriate commands and deps,
  30.    or returns 0 if no implicit rule was found.  */
  31.  
  32. int
  33. try_implicit_rule (file, depth)
  34.      struct file *file;
  35.      unsigned int depth;
  36. {
  37.   DEBUGPR ("Looking for an implicit rule for `%s'.\n");
  38.  
  39. #ifndef    NO_ARCHIVES
  40.   /* If this is an archive member reference, use just the
  41.      archive member name to search for implicit rules.  */
  42.   if (ar_name (file->name))
  43.     {
  44.       DEBUGPR ("Looking for archive-member implicit rule for `%s'.\n");
  45.       if (pattern_search (file, 1, depth, 0))
  46.     return 1;
  47.     }
  48. #endif
  49.  
  50.   return pattern_search (file, 0, depth, 0);
  51. }
  52.  
  53. #define DEBUGP2(msg, a1, a2) \
  54.   if (debug_flag) \
  55.     { print_spaces (depth); printf (msg, a1, a2); fflush (stdout);  } else
  56.  
  57. /* Search the pattern rules for a rule with an existing dependency to make
  58.    FILE.  If a rule is found, the appropriate commands and deps are put in FILE
  59.    and 1 is returned.  If not, 0 is returned.
  60.  
  61.    If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)".  A rule for
  62.    "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
  63.    directory and filename parts.
  64.  
  65.    If an intermediate file is found by pattern search, the intermediate file
  66.    is set up as a target by the recursive call and is also made a dependency
  67.    of FILE.
  68.  
  69.    DEPTH is used for debugging messages.  */
  70.  
  71. static int
  72. pattern_search (file, archive, depth, recursions)
  73.      struct file *file;
  74.      int archive;
  75.      unsigned int depth;
  76.      unsigned int recursions;
  77. {
  78.   /* Filename we are searching for a rule for.  */
  79.   char *filename = archive ? index (file->name, '(') : file->name;
  80.  
  81.   /* Length of FILENAME.  */
  82.   unsigned int namelen = strlen (filename);
  83.  
  84.   /* The last slash in FILENAME (or nil if there is none).  */
  85.   char *lastslash;
  86.  
  87.   /* This is a file-object used as an argument in
  88.      recursive calls.  It never contains any data
  89.      except during a recursive call.  */
  90.   struct file *intermediate_file = 0;
  91.  
  92.   /* List of dependencies found recursively.  */
  93.   struct file **intermediate_files
  94.     = (struct file **) alloca (max_pattern_deps * sizeof (struct file *));
  95.  
  96.   /* List of the patterns used to find intermediate files.  */
  97.   char **intermediate_patterns
  98.     = (char **) alloca (max_pattern_deps * sizeof (char *));
  99.  
  100.   /* This buffer records all the dependencies actually found for a rule.  */
  101.   char **found_files = (char **) alloca (max_pattern_deps * sizeof (char *));
  102.   /* Number of dep names now in FOUND_FILES.  */
  103.   unsigned int deps_found;
  104.  
  105.   /* Names of possible dependencies are constructed in this buffer.  */
  106.   register char *depname = (char *) alloca (namelen + max_pattern_dep_length);
  107.  
  108.   /* The start and length of the stem of FILENAME for the current rule.  */
  109.   register char *stem;
  110.   register unsigned int stemlen;
  111.  
  112.   /* Buffer in which we store all the rules that are possibly applicable.  */
  113.   struct rule **tryrules
  114.     = (struct rule **) alloca (num_pattern_rules * sizeof (struct rule *));
  115.  
  116.   /* Number of valid elements in TRYRULES.  */
  117.   unsigned int nrules;
  118.  
  119.   /* The numbers of the rule targets of each rule
  120.      in TRYRULES that matched the target file.  */
  121.   unsigned int *matches
  122.     = (unsigned int *) alloca (num_pattern_rules * sizeof (unsigned int));
  123.  
  124.   /* Each element is nonzero if LASTSLASH was used in
  125.      matching the corresponding element of TRYRULES.  */
  126.   char *checked_lastslash
  127.     = (char *) alloca (num_pattern_rules * sizeof (char));
  128.  
  129.   /* The index in TRYRULES of the rule we found.  */
  130.   unsigned int foundrule;
  131.  
  132.   /* Nonzero if should consider intermediate files as dependencies.  */
  133.   int intermed_ok;
  134.  
  135.   /* Nonzero if we have matched a pattern-rule target
  136.      that is not just `%'.  */
  137.   int specific_rule_matched = 0;
  138.  
  139.   register unsigned int i;
  140.   register struct rule *rule;
  141.   register struct dep *dep;
  142.  
  143.   char *p;
  144.  
  145. #ifndef    NO_ARCHIVES
  146.   if (archive || ar_name (filename))
  147.     lastslash = 0;
  148.   else
  149. #endif
  150.     {
  151.       /* Set LASTSLASH to point at the last slash in FILENAME
  152.      but not counting any slash at the end.  (foo/bar/ counts as
  153.      bar/ in directory foo/, not empty in directory foo/bar/.)  */
  154.       lastslash = rindex (filename, '/');
  155.       if (lastslash != 0 && lastslash[1] == '\0')
  156.     lastslash = 0;
  157.     }
  158.  
  159.   /* First see which pattern rules match this target
  160.      and may be considered.  Put them in TRYRULES.  */
  161.  
  162.   nrules = 0;
  163.   for (rule = pattern_rules; rule != 0; rule = rule->next)
  164.     {
  165.       int specific_rule_may_have_matched = 0;
  166.       int check_lastslash;
  167.  
  168.       /* If the pattern rule has deps but no commands, ignore it.
  169.      Users cancel built-in rules by redefining them without commands.  */
  170.       if (rule->deps != 0 && rule->cmds == 0)
  171.     continue;
  172.  
  173.       /* If this rule is in use by a parent pattern_search,
  174.      don't use it here.  */
  175.       if (rule->in_use)
  176.     {
  177.       DEBUGP2 ("Avoiding implicit rule recursion.\n", 0, 0);
  178.       continue;
  179.     }
  180.  
  181.       for (i = 0; rule->targets[i] != 0; ++i)
  182.     {
  183.       char *target = rule->targets[i];
  184.       char *suffix = rule->suffixes[i];
  185.  
  186.       /* Rules that can match any filename and are not terminal
  187.          are ignored if we're recursing, so that they cannot be
  188.          intermediate files.  */
  189.       if (recursions > 0 && target[1] == '\0' && !rule->terminal)
  190.         continue;
  191.  
  192.       if (rule->lens[i] > namelen)
  193.         /* It can't possibly match.  */
  194.         continue;
  195.  
  196.       /* From the lengths of the filename and the pattern parts,
  197.          find the stem: the part of the filename that matches the %.  */
  198.       stem = filename + (suffix - target - 1);
  199.       stemlen = namelen - rule->lens[i] + 1;
  200.  
  201.       /* Set CHECK_LASTSLASH if FILENAME contains a directory
  202.          prefix and the target pattern does not contain a slash.  */
  203.  
  204.       check_lastslash = lastslash != 0 && index (target, '/') == 0;
  205.       if (check_lastslash)
  206.         {
  207.           /* In that case, don't include the
  208.          directory prefix in STEM here.  */
  209.           unsigned int difference = lastslash - filename + 1;
  210.           if (difference > stemlen)
  211.         continue;
  212.           stemlen -= difference;
  213.           stem += difference;
  214.         }
  215.  
  216.       /* Check that the rule pattern matches the text before the stem.  */
  217.       if (check_lastslash)
  218.         {
  219.           if (stem > (lastslash + 1)
  220.           && strncmp (target, lastslash + 1, stem - lastslash - 1))
  221.         continue;
  222.         }
  223.       else if (stem > filename
  224.            && strncmp (target, filename, stem - filename))
  225.         continue;
  226.  
  227.       /* Check that the rule pattern matches the text after the stem.
  228.          We could test simply use streq, but this way we compare the
  229.          first two characters immediately.  This saves time in the very
  230.          common case where the first character matches because it is a
  231.          period.  */
  232.       if (*suffix != stem[stemlen]
  233.           || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
  234.         continue;
  235.  
  236.       /* Record if we match a rule that not all filenames will match.  */
  237.       if (target[1] != '\0')
  238.         specific_rule_may_have_matched = 1;
  239.  
  240.       /* We have a matching target.  Don't search for any more.  */
  241.       break;
  242.     }
  243.  
  244.       /* None of the targets matched.  */
  245.       if (rule->targets[i] == 0)
  246.     continue;
  247.  
  248.       specific_rule_matched |= specific_rule_may_have_matched;
  249.  
  250.       /* A rule with no dependencies and no commands exists solely to set
  251.      specific_rule_matched when it matches.  Don't try to use it.  */
  252.       if (rule->deps == 0 && rule->cmds == 0)
  253.     continue;
  254.  
  255.       /* Record this rule in TRYRULES and the index
  256.      of the (first) matching target in MATCHES.  */
  257.       tryrules[nrules] = rule;
  258.       matches[nrules] = i;
  259.       checked_lastslash[nrules] = check_lastslash;
  260.       ++nrules;
  261.     }
  262.  
  263.   /* If we have found a matching rule that won't match all filenames,
  264.      retroactively reject any "terminal" rules that do always match.  */
  265.   if (specific_rule_matched)
  266.     for (i = 0; i < nrules; ++i)
  267.       if (!tryrules[i]->terminal)
  268.     {
  269.       register unsigned int j;
  270.       for (j = 0; tryrules[i]->targets[j] != 0; ++j)
  271.         if (tryrules[i]->targets[j][1] == '\0')
  272.           break;
  273.       if (tryrules[i]->targets[j] != 0)
  274.         tryrules[i] = 0;
  275.     }
  276.  
  277.   /* Try each rule once without intermediate files, then once with them.  */
  278.   for (intermed_ok = 0; intermed_ok == !!intermed_ok; ++intermed_ok)
  279.     {
  280.       /* Try each pattern rule till we find one that applies.
  281.      If it does, copy the names of its dependencies (as substituted)
  282.      and store them in FOUND_FILES.  DEPS_FOUND is the number of them.  */
  283.  
  284.       for (i = 0; i < nrules; i++)
  285.     {
  286.       int check_lastslash;
  287.  
  288.       rule = tryrules[i];
  289.  
  290.       /* RULE is nil when we discover that a rule,
  291.          already placed in TRYRULES, should not be applied.  */
  292.       if (rule == 0)
  293.         continue;
  294.  
  295.       /* Reject any terminal rules if we're
  296.          looking to make intermediate files.  */
  297.       if (intermed_ok && rule->terminal)
  298.         continue;
  299.  
  300.       /* Mark this rule as in use so a recursive
  301.          pattern_search won't try to use it.  */
  302.       rule->in_use = 1;
  303.  
  304.       /* From the lengths of the filename and the matching pattern parts,
  305.          find the stem: the part of the filename that matches the %.  */
  306.       stem = filename
  307.         + (rule->suffixes[matches[i]] - rule->targets[matches[i]]) - 1;
  308.       stemlen = namelen - rule->lens[matches[i]] + 1;
  309.       check_lastslash = (lastslash != 0
  310.                  && index (rule->targets[matches[i]], '/') == 0);
  311.       if (check_lastslash)
  312.         {
  313.           stem += lastslash - filename + 1;
  314.           stemlen -= (lastslash - filename) + 1;
  315.         }
  316.  
  317.       DEBUGP2 ("Trying pattern rule with stem `%.*s'.\n",
  318.            stemlen, stem);
  319.  
  320.       /* Try each dependency; see if it "exists".  */
  321.  
  322.       deps_found = 0;
  323.       for (dep = rule->deps; dep != 0; dep = dep->next)
  324.         {
  325.           /* If the dependency name has a %, substitute the stem.  */
  326.           p = index (dep_name (dep), '%');
  327.           if (p != 0)
  328.         {
  329.           register unsigned int i;
  330.           if (check_lastslash)
  331.             {
  332.               i = lastslash - filename + 1;
  333.               bcopy (filename, depname, i);
  334.             }
  335.           else
  336.             i = 0;
  337.           bcopy (dep_name (dep), depname + i, p - dep_name (dep));
  338.           i += p - dep_name (dep);
  339.           bcopy (stem, depname + i, stemlen);
  340.           i += stemlen;
  341.           strcpy (depname + i, p + 1);
  342.           p = depname;
  343.         }
  344.           else
  345.         p = dep_name (dep);
  346.  
  347.           /* P is now the actual dependency name as substituted.  */
  348.  
  349.           if (file_impossible_p (p))
  350.         {
  351.           /* If this dependency has already been ruled
  352.              "impossible", then the rule fails and don't
  353.              bother trying it on the second pass either
  354.              since we know that will fail too.  */
  355.           DEBUGP2 ("Rejecting impossible %s dependent `%s'.\n",
  356.                p == depname ? "implicit" : "rule", p);
  357.           tryrules[i] = 0;
  358.           break;
  359.         }
  360.  
  361.           intermediate_files[deps_found] = 0;
  362.  
  363.           DEBUGP2 ("Trying %s dependency `%s'.\n",
  364.                p == depname ? "implicit" : "rule", p);
  365.           if (!rule->subdir && lookup_file (p) != 0 || file_exists_p (p))
  366.         {
  367.           found_files[deps_found++] = savestring (p, strlen (p));
  368.           continue;
  369.         }
  370.           /* This code, given FILENAME = "lib/foo.o", dependency name
  371.          "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c".  */
  372.           if (vpath_search (&p))
  373.         {
  374.           DEBUGP2 ("Found dependent as `%s'.\n", p, 0);
  375.           found_files[deps_found++] = p;
  376.           continue;
  377.         }
  378.  
  379.           /* We could not find the file in any place we should look.
  380.          Try to make this dependency as an intermediate file,
  381.          but only on the second pass.  */
  382.  
  383.           if (intermed_ok && p == depname)
  384.         {
  385.           if (intermediate_file == 0)
  386.             intermediate_file
  387.               = (struct file *) alloca (sizeof (struct file));
  388.  
  389.           DEBUGP2 ("Looking for a rule with intermediate file `%s'.\n",
  390.                p, 0);
  391.  
  392.           bzero ((char *) intermediate_file, sizeof (struct file));
  393.           intermediate_file->name = p;
  394.           if (pattern_search (intermediate_file, 0, depth + 1,
  395.                       recursions + 1))
  396.             {
  397.               p = savestring (p, strlen (p));
  398.               intermediate_patterns[deps_found]
  399.             = intermediate_file->name;
  400.               found_files[deps_found] = p;
  401.               intermediate_file->name = p;
  402.               intermediate_files[deps_found] = intermediate_file;
  403.               intermediate_file = 0;
  404.               ++deps_found;
  405.               continue;
  406.             }
  407.  
  408.           /* If we have tried to find P as an intermediate
  409.              file and failed, mark that name as impossible
  410.              so we won't go through the search again later.  */
  411.           file_impossible (p);
  412.         }
  413.  
  414.           /* A dependency of this rule does not exist.
  415.          Therefore, this rule fails.  */
  416.           break;
  417.         }
  418.  
  419.       /* This rule is no longer `in use' for recursive searches.  */
  420.       rule->in_use = 0;
  421.  
  422.       if (dep != 0)
  423.         {
  424.           /* This pattern rule does not apply.
  425.          If some of its dependencies succeeded,
  426.          free the data structure describing them.  */
  427.           while (deps_found-- > 0)
  428.         {
  429.           register struct file *f = intermediate_files[deps_found];
  430.           free (found_files[deps_found]);
  431.           if (f != 0
  432.               && (f->stem < f->name
  433.               || f->stem > f->name + strlen (f->name)))
  434.             free (f->stem);
  435.         }
  436.         }
  437.       else
  438.         /* This pattern rule does apply.  Stop looking for one.  */
  439.         break;
  440.     }
  441.  
  442.       /* If we found an applicable rule without
  443.      intermediate files, don't try with them.  */
  444.       if (i < nrules)
  445.     break;
  446.  
  447.       rule = 0;
  448.     }
  449.  
  450.   /* RULE is nil if the loop went all the way
  451.      through the list and everything failed.  */
  452.   if (rule == 0)
  453.     return 0;
  454.  
  455.   foundrule = i;
  456.  
  457.   /* If we are recursing, store the pattern that matched
  458.      FILENAME in FILE->name for use in upper levels.  */
  459.  
  460.   if (recursions > 0)
  461.     /* Kludge-o-matic */
  462.     file->name = rule->targets[matches[foundrule]];
  463.  
  464.   /* FOUND_FILES lists the dependencies for the rule we found.
  465.      This includes the intermediate files, if any.
  466.      Convert them into entries on the deps-chain of FILE.  */
  467.  
  468.   while (deps_found-- > 0)
  469.     {
  470.       register char *s;
  471.  
  472.       if (intermediate_files[deps_found] != 0)
  473.     {
  474.       /* If we need to use an intermediate file,
  475.          make sure it is entered as a target, with the info that was
  476.          found for it in the recursive pattern_search call.
  477.          We know that the intermediate file did not already exist as
  478.          a target; therefore we can assume that the deps and cmds
  479.          of F below are null before we change them.  */
  480.  
  481.       struct file *imf = intermediate_files[deps_found];
  482.       register struct file *f = enter_file (imf->name);
  483.       f->deps = imf->deps;
  484.       f->cmds = imf->cmds;
  485.       f->stem = imf->stem;
  486.       imf = lookup_file (intermediate_patterns[deps_found]);
  487.       if (imf != 0 && imf->precious)
  488.         f->precious = 1;
  489.       f->intermediate = 1;
  490.       f->tried_implicit = 1;
  491.       for (dep = f->deps; dep != 0; dep = dep->next)
  492.         {
  493.           dep->file = enter_file (dep->name);
  494.           dep->name = 0;
  495.           dep->file->tried_implicit |= dep->changed;
  496.         }
  497.       num_intermediates++;
  498.     }
  499.  
  500.       dep = (struct dep *) xmalloc (sizeof (struct dep));
  501.       s = found_files[deps_found];
  502.       if (recursions == 0)
  503.     {
  504.       dep->name = 0;
  505.       dep->file = enter_file (s);    
  506.     }
  507.       else
  508.     {
  509.       dep->name = s;
  510.       dep->file = 0;
  511.       dep->changed = 0;
  512.     }
  513.       if (intermediate_files[deps_found] == 0 && tryrules[foundrule]->terminal)
  514.     {
  515.       /* If the file actually existed (was not an intermediate file),
  516.          and the rule that found it was a terminal one, then we want
  517.          to mark the found file so that it will not have implicit rule
  518.          search done for it.  If we are not entering a `struct file' for
  519.          it now, we indicate this with the `changed' flag.  */
  520.       if (dep->file == 0)
  521.         dep->changed = 1;
  522.       else
  523.         dep->file->tried_implicit = 1;
  524.     }
  525.       dep->next = file->deps;
  526.       file->deps = dep;
  527.     }
  528.  
  529.   uniquize_deps (file->deps);
  530.  
  531.   if (!checked_lastslash[foundrule])
  532.     file->stem = stem[stemlen] == '\0' ? stem : savestring (stem, stemlen);
  533.   else
  534.     {
  535.       file->stem = (char *) xmalloc (((lastslash + 1) - filename)
  536.                      + stemlen + 1);
  537.       bcopy (filename, file->stem, (lastslash + 1) - filename);
  538.       bcopy (stem, file->stem + ((lastslash + 1) - filename), stemlen);
  539.       file->stem[((lastslash + 1) - filename) + stemlen] = '\0';
  540.     }
  541.  
  542.   file->cmds = rule->cmds;
  543.  
  544.   /* Put the targets other than the one that
  545.      matched into FILE's `also_make' member.  */
  546.  
  547.   /* If there was only one target, there is nothing to do.  */
  548.   if (rule->targets[1] != 0)
  549.     {
  550.       unsigned int max_targets = 2;
  551.       register unsigned int idx;
  552.  
  553.       file->also_make = (char **) xmalloc (2 * sizeof (char *));
  554.  
  555.       idx = 0;
  556.       for (i = 0; rule->targets[i] != 0; ++i)
  557.     if (i != matches[foundrule])
  558.       {
  559.         if (idx == max_targets - 1)
  560.           {
  561.         max_targets += 5;
  562.         file->also_make
  563.           = (char **) xrealloc ((char *) file->also_make,
  564.                     max_targets * sizeof (char *));
  565.           }
  566.  
  567.         p = file->also_make[idx++] = (char *) xmalloc (rule->lens[i] +
  568.                                stemlen + 1);
  569.         bcopy (rule->targets[i], p,
  570.            rule->suffixes[i] - rule->targets[i] - 1);
  571.         p += rule->suffixes[i] - rule->targets[i] - 1;
  572.         bcopy (stem, p, stemlen);
  573.         p += stemlen;
  574.         bcopy (rule->suffixes[i], p,
  575.            rule->lens[i]
  576.            - (rule->suffixes[i] - rule->targets[i] - 1) + 1);
  577.       }
  578.  
  579.       file->also_make[idx] = 0;
  580.       if (idx < max_targets - 1)
  581.     file->also_make = (char **) xrealloc ((char *) file->also_make,
  582.                           (idx + 1) * sizeof (char *));
  583.     }
  584.  
  585.   return 1;
  586. }
  587.